package com.hgw.easyexcel3_demo.util;

import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.*;

/**
 * Description: 单元格处理程序（可在此编单元格样式）
 *
 * @author LinHuiBa-YanAn
 * @date 2023/5/24 15:44
 */
public class CellWriteHandlerUtil implements CellWriteHandler {

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        if (BooleanUtils.isTrue(context.getHead())) {
            Row row = context.getRow();
            WriteCellData<?> cellData = context.getFirstCellData();
            WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();
            writeCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
            writeCellStyle.setBorderLeft(null);
            writeCellStyle.setBorderRight(null);
            writeCellStyle.setBorderTop(null);
            writeCellStyle.setBorderBottom(null);
            WriteFont writeFont = writeCellStyle.getWriteFont();
            writeFont.setFontName("宋体");
            writeFont.setFontHeightInPoints((short) 11);
            writeFont.setBold(Boolean.FALSE);
            Integer rowIndex = context.getRowIndex();
            if (rowIndex.equals(0)) {
                // 提示信息设置
                row.setHeightInPoints(120);
                writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND
                writeCellStyle.setFillPatternType(FillPatternType.NO_FILL);
            } else {
                row.setHeightInPoints(17);
                writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW1.getIndex());
            }
        }
    }

}
